//index.js
//获取应用实例
import Dialog from '../../plugins/vant-weapp/dist/dialog/dialog';
import { getQueryString } from '../../utils/util.js'
const app = getApp()
const api = require("../../utils/api.js")

Page({
  data: {
    motto: 'Hello World',
    userInfo: app.globalData.userInfo,
    swUser: app.globalData.swUser,
    hasUserInfo: app.globalData.hasUserInfo,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    vip_remain_text: '非会员或会员已到期',
    vipEndDays: 0,
    banners: ['/images/top-banner-1.png', '/images/top-banner-2.png'],
    swiperHeight: 100,
    dno: null
  },
  onLoad: function(options) {
    var dno = options.scene ? options.scene : null
    var userInfo = wx.getStorageSync('swUser'),
      that = this
    // if (!userInfo) {
    //   var url = '/pages/login/index'
    //   if (dno) {
    //     url += '?dno=' + dno
    //   }
    //   wx.navigateTo({
    //     url: url,
    //   })
    // }
    if (dno) {
      this.setData({
        dno: dno
      })
      app.startDevice(that.data.dno)
    }
    dno = wx.getStorageSync('start-dno')
    if (dno) {
      this.setData({
        dno: dno
      })
      app.startDevice(that.data.dno)
    }
  },
  startDevice(dno) {
    var that = this,
      vipEndDays = this.data.vipEndDays
    if (!dno) {
      wx.showToast({
        icon: 'none',
        title: '请扫码后启动设备',
      })
      return false
    }

    // if (vipEndDays <= 0) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '非会员不能启动设备',
    //   })
    //   return false
    // } else {
    wx.showLoading({
      title: '设备启动中',
    })
    wx.request({
      url: api.startDeviceUrl,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        DNO: dno
      },
      dataType: 'json',
      success: function(res) {
        wx.hideLoading()
        if (res.data.Code === 1) {
          wx.showToast({
            title: '启动成功',
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.Msg,
          })
        }
      },
      fail: function(res) {
        wx.hideLoading()
        wx.showToast({
          title: '启动失败',
        })
      },
      complete: function(res) {
        wx.setStorageSync('start-dno', null)
      }
    })
    // }
  },
  imageLoad: function(e) {
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    this.setData({
      swiperHeight: imgheight
    })
  },
  onShow: function(e) {
    app.updateVIP(this);
    var vip_remain_text = '非会员或会员已到期'
    this.setData({
      userInfo: app.globalData.userInfo,
      swUser: app.globalData.swUser,
      hasUserInfo: app.globalData.hasUserInfo,
      vipEndDays: app.globalData.vipEndDays
    });
  },
  scanCode: function() {
    wx.scanCode({
      success(res) {
        if (res.errMsg == "scanCode:ok") {
          var dno = getQueryString('scene', res.path)
          console.log(dno)
          if (dno) {
            app.startDevice(dno)
          }
        }
      }
    });
  },
  recharge: function() {
    wx.navigateTo({
      url: '/pages/recharge/index'
    });
  },
  share: function(res) {
    wx.navigateTo({
      url: '/pages/share/index',
    })
  }
})